﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using GlobalResources;

namespace Googler.BLL
{
    public partial class News
    {
        const string body = "<HTML><body><h2>{0}</h2><div>{0}</div></body></HTML>";
        readonly string SummeryBody;

        
        public News()
        {

            SummeryBody = "<hr /><b>{0}&nbsp;&nbsp;<a href='/PrintPreview.aspx?id={0}' style='font-size:7pt'>نسخة للطباعة</a></b>" +
                          "&nbsp;&nbsp;" + 
                          "&nbsp;&nbsp;" +
                          "<h2><a href='{3}' style=''>{1}</a></h2><div>{2}</div>";
        }
        public News(long oid)
            : this()
        {
            OId = oid;
        }
        public News(XElement atom)
            : this()
        {
            XNamespace ns = atom.Name.Namespace;
            Id = atom.Element(ns + "id").Value;
            Title = atom.Element(ns + "title").Value;
            Updated = Convert.ToDateTime(atom.Element(ns + "updated").Value);
            Link = getUrl(atom.Element(ns + "link").Attribute("href").Value);//atom.Element(ns + "link").Attribute("href").Value;
            Summery = atom.Element(ns + "content").Value;
            Source = getSource(Link);
            IsFullText = false;
        }

        private string parseSummery(string p)
        {
            return p;
        }

        private string getSource(string Link)
        {
            Uri uri = new Uri(Link);
            return uri.Host;
        }

        private string getUrl(string url)
        {
            try
            {
                int i = url.IndexOf("url=");
                url = System.Web.HttpUtility.UrlDecode(url.Substring(i + "url=".Length));
                return url;
            }
            catch { return url; }
        }
        public long OId { get; set; }
        public string Id { get; set; }
        public string Title { get; set; }
        public DateTime Updated { get; set; }
        public string Link { get; set; }
        public string Summery { get; set; }
        public string FullText { get; set; }
        public string Source { get; set; }
        public bool IsFullText { get; set; }
        public int LanguageID { get; set; }

        public string RenderSummeries()
        {
            return string.Format(SummeryBody, this.OId, Title, Summery,Link);
        }
    }
    public class EntryList : Dictionary<string, News>
    {

    }
}
